package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.commom.ResultInfo;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.service.SetmealService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Sea
 * @date 2023/3/5 18:03
 */
//套餐模块
@Api(tags = "套餐")
@RestController
@Slf4j
@Cacheable
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    //套餐列表
    @GetMapping("/setmeal/page")
    public ResultInfo findByPage(@RequestParam(name = "page",defaultValue = "1")Integer pageNum,
                                 @RequestParam(name = "pageSize",defaultValue = "10")Integer pageSize,String name){
        Page<Setmeal>page =setmealService.findByPage(pageNum,pageSize,name);
        return ResultInfo.success(page);
    }
   //新增套餐
   @CacheEvict(value = "setmeal",key = "#setmeal.categoryId")
   @PostMapping("/setmeal")
    public ResultInfo save(@RequestBody Setmeal setmeal){
        setmealService.sacve(setmeal);
        return ResultInfo.success();
    }
    @GetMapping("/setmeal/{id}")
    public  ResultInfo findById(@PathVariable(value = "id")Long id){


        Setmeal setmeal =setmealService.findById(id);
        return ResultInfo.success(setmeal);
    }
    //修改
    @CacheEvict(value = "setmeal",allEntries = true)
    @PutMapping("/setmeal")
    public ResultInfo updateSetmeal(@RequestBody Setmeal setmeal){
        //调用service
        setmealService.updateSetmeal(setmeal);

        return ResultInfo.success();
    }
    //起售,停售
    @PostMapping("/setmeal/status/{status}")
    @CacheEvict(value = "setmeal",allEntries = true)
    public ResultInfo updateStatusSetmeal(@PathVariable(value = "status") Integer status,
                                          @RequestParam List<Long> ids){
        //调用service
        setmealService.updateStatusSetmeal(status,ids);

        return ResultInfo.success();
    }
    // 删除套餐
    @CacheEvict(value = "setmeal",allEntries = true)
    @DeleteMapping("/setmeal")
    public ResultInfo deleteBatch(@RequestParam List<Long> ids) { // 1.接收参数
        // 2.调用service删除
        setmealService.deleteBatch(ids);
        // 3.返回结果
        return ResultInfo.success();
    }
}

